package erfgame.core.graphics.renderer;


import erfgame.core.Rotation;
import erfgame.core.entity.Entity;
import erfgame.core.entity.Orientable;
import erfgame.core.world.World;

public class OrientableEntityRendererDelegateFactory implements
		EntityRendererFactory {

	private static final Rotation ROTATIONS[] = {Rotation.degrees0, Rotation.degrees90, Rotation.degrees180, Rotation.degrees270};
	private EntityRendererFactory[] rendererFactories;
	
	public OrientableEntityRendererDelegateFactory( EntityRendererFactory[] rendererFactories ) {
		this.rendererFactories = rendererFactories;
	}
	
	public EntityRenderer getRenderer(World world, Entity entity, Rotation viewRotation ) {
		EntityRenderer[] renderers = getRenderers( world, entity );
		return new OrientableEntityRendererDelegate(
				renderers, 
				(Orientable)entity, 
				viewRotation
		);
	}
	
	protected EntityRenderer[] getRenderers( World world, Entity entity) {
		EntityRenderer[] renderers = new EntityRenderer[rendererFactories.length];
		for( int i=rendererFactories.length; i>0; ) {
			i--;
			EntityRendererFactory rendererFactory = rendererFactories[i];
			Rotation rotation = ROTATIONS[i];
			renderers[i] = rendererFactory.getRenderer(world, entity, rotation);
		}
		return renderers;
	}

}
